import request from '@/utils/request';
import { 
  TableListParams, TagListParams,UserListItem,
  RoleListItem,RoleListParams,
  OrgItem, OrgTimeItem,OrgTypeItem, OrgListParams,
  VersionistParams ,VersionListItem,
  DeviceAuthListParams, DeviceAuthListItem,
  OrgOrderListItemParams,OrgOrderListItem,
  setupItem
} from './data.d';
import qs from 'qs';
import { parseInt } from 'lodash';

export async function queryRule(params?: TableListParams) {
  return request('/api/rule', {
    params,
  });
}

const getRandomuserParams = (params: any) => {
  return {
    pageNo: params.current,
    ...params,
  };
};
/**
 * 
 * 用户
 */
//用户列表
export async function userList(params: TableListParams) {
  params = getRandomuserParams(params);
  const res = request('/api/user/list', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  });
  return res.then(function (response) {
    if (response && response.code === 200) {
      //数据转换
      const obj = new Object();
      let list = response.data.list;
      for (var i in list) {
        list[i].key = list[i].id;
        list[i].index = parseInt(i)+1;
      }
      obj['data'] = response.data.list;
      obj['pageCount'] = response.data.pageCount;
      obj['current'] = response.data.pageNo;
      obj['total'] = response.data.totalCount;
      return obj;
    }
  });
}

//添加用户
export async function addUser(params: TableListParams) {
  return request('/api/user/add', {
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}

//编辑用户
export async function updateUser(params: TableListParams) {
  return request('/api/user/update', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}

//删除用户
export async function userRemove(id: number) {
  return request(`/api/user/delete/${id}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    method: 'delete',
  });
}

//用户启停
export async function updateState(id: number) {
  return request(`/api/user/disable/${id}`, {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });
}
//用户--通知 获取用户异常通知时间设置信息
export async function getUserAlert(id: number) {
  return request(`/api/user/getAlert/${id}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}
//用户--通知 设置通知邮件
export async function setUserAlert(params: UserListItem) {
  return request('/api/user/setAlert', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data:{
      ...params,
    }
  });
}
/**
 * 
 * 权限
 */
//权限列表--查看权限树
export async function getAllPermission(roleId:number) {//当前用户登录的roleId
  return request(`/api/permission/permissionTree/${roleId}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}
//权限列表--编辑添加权限树
export async function getCurrentPermission(topOrgId:number) {//所属机构的topOrgId
  return request(`/api/permission/orgPermissionTree/${topOrgId}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}
/**
 * 
 * 角色
 */
//角色列表
export async function roleList(params: RoleListParams) {
  params = getRandomuserParams(params);
  const res = request('/api/role/list', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  });
  return res.then(function (response) {
    if (response && response.code === 200) {
      //数据转换
      const obj = new Object();
      let list = response.data.list;
      for (var i in list) {
        list[i].key = list[i].id;
        list[i].index = parseInt(i)+1;
      }
      obj['data'] = response.data.list;
      obj['pageCount'] = response.data.pageCount;
      obj['current'] = response.data.pageNo;
      obj['total'] = response.data.totalCount;
      return obj;
    }
  });
}

//添加角色
export async function addRole(params: RoleListItem) {
  return request('/api/role/add', {
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}

//编辑角色
export async function updateRole(params: RoleListItem) {
  return request('/api/role/update', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}
//角色删除
export async function roleDel(id: number) {
  return request(`/api/role/delete/${id}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    method: 'delete',
  });
}
/**
 * 
 * 标签
 */
//标签列表
export async function tagList(params: TagListParams) {
  params = getRandomuserParams(params);
  const res = request('/api/tag/list', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  });
  return res.then(function (response) {
    if (response && response.code === 200) {
      //数据转换
      const obj = new Object();
      let list = response.data.list;
      for (var i in list) {
        list[i].key = list[i].id;
      }
      obj['data'] = response.data.list;
      obj['pageCount'] = response.data.pageCount;
      obj['current'] = response.data.pageNo;
      obj['total'] = response.data.totalCount;
      return obj;
    }
  });
}

//公共分类 --标签列表
export async function getTagList(params:any) {
  return request('/api/tag/get', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  });
}

//添加标签
export async function addTag(params: TagListParams) {
  return request('/api/tag/add', {
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}

//编辑标签
export async function updateTag(params: TagListParams) {
  return request('/api/tag/update', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}

//删除标签
export async function tagDel(params: any) {
  return request('/api/tag/delete', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    method: 'delete',
    params,
  });
}
//批量删除标签
export async function batchDelete(params: any) {
  return request('/api/tag/batchDelete/', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    method: 'delete',
    params,
  });
}
/**
 * 
 * 机构
 */
//机构列表
export async function orgList(params: OrgListParams) {
  params = getRandomuserParams(params);
  const res = request('/api/org/list', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  });
  return res.then(function (response) {
    if (response && response.code === 200) {
      //数据转换
      const obj = new Object();
      let list = response.data.list;
      for (var i in list) {
        list[i].key = list[i].id;
        list[i].index = parseInt(i)+1;
      }
      obj['data'] = response.data.list;
      obj['pageCount'] = response.data.pageCount;
      obj['current'] = response.data.pageNo;
      obj['total'] = response.data.totalCount;
      return obj;
    }
  });
}
//机构--添加
export async function addOrg(params: OrgItem) {
  return request('/api/org/add', {
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}
//机构--编辑
export async function updateOrg(params: OrgItem) {
  return request('/api/org/update', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}
//机构--删除
export async function delOrg(id: number) {
  return request(`/api/org/delete/${id}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    method: 'delete',
  });
}
//机构--设置 获取机构异常通知时间设置信息
export async function getAlertDetail(orgId: number) {
  return request(`/api/org/getAlertDetail/${orgId}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}
//机构--设置 设置报警邮件
export async function setEmailAlert(params: any) {
  return request('/api/org/setEmailAlert', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data:{
      ...params,
    }
  });
}
//机构--设置 设置报警企业微信
export async function setWeChatUserAlert(params: any) {
  return request('/api/org/setWechatUserAlert', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data:{
      ...params,
    }
  });
}
//机构--设置 设置报警企业微信群发
export async function setWeChatGroupAlert(params: any) {
  return request('/api/org/setWechatGroupAlert', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data:{
      ...params,
    }
  });
}
//机构--设置 设置钉钉
export async function setDingDingAlert(params: any) {
  return request('/api/org/setDingdingAlert', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data:{
      ...params,
    }
  });
}
//上级机构列表
export async function getOrgTreeList() {
  return request('/api/org/listTreeOrgs', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}
//所属机构列表
export async function getFirstLevelOrgId() {
  return request('/api/org/getFirstLevelOrgId', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}
//所属分组列表
export async function getGroupList(orgId:number) {
  return request(`/api/device/group/listGroupByOrg/${orgId}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}
//所属角色下拉框
export async function getRoleList(topOrgId:number) {
  return request(`/api/role/listAll/${topOrgId}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}
//机构类型列表
export async function getOrgTypeList() {
  return request('/api/organizationType/list', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    // params,
  });
}
//机构类型列表--下拉框
export async function getOrgTypeSelectList(topOrgId:number) {
  return request(`/api/organizationType/listByTopOrgId/${topOrgId}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}
//机构类型--编辑
export async function updateOrgType(params: OrgTypeItem) {
  return request('/api/organizationType/update', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}
//机构类型--添加
export async function addOrgType(params: OrgTypeItem) {
  return request('/api/organizationType/add', {
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}
//机构类型--删除
export async function delOrgType(id: number) {
  return request(`/api/organizationType/delete/${id}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    method: 'delete',
  });
}
/**
 * 
 * 升级管理
 */
//升级列表
export async function versionList(params: VersionistParams) {
  params = getRandomuserParams(params);
  const res = request('/api/system/apk_version/listPage', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  });
  return res.then(function (response) {
    if (response && response.code === 200) {
      const obj = new Object();
      let list = response.data.list;
      for (var i in list) {
        list[i].key = list[i].id;
        list[i].index = parseInt(i)+1;
      }
      obj['data'] = response.data.list;
      obj['pageCount'] = response.data.pageCount;
      obj['current'] = response.data.pageNo;
      obj['total'] = response.data.totalCount;
      return obj;
    }
  });
}
//版本删除
export async function versionDel(id: number) {
  return request(`/api/system/apk_version/delete/${id}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    method: 'delete',
  });
}
//版本--禁用
export async function versionDisable(params: string) {
  return request(`/api/system/apk_version/disable/${(params)}`, {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  });
}
/**
 * 
 * 认证设备管理
 */
//认证设备列表
export async function deviceAuthList(params: DeviceAuthListParams) {
  params = getRandomuserParams(params);
  const res = request('/api/system/deviceAuth/list', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  });
  return res.then(function (response) {
    if (response && response.code === 200) {
      const obj = new Object();
      let list = response.data.list;
      for (var i in list) {
        list[i].key = list[i].id;
        list[i].index = parseInt(i)+1;
      }
      obj['data'] = response.data.list;
      obj['pageCount'] = response.data.pageCount;
      obj['current'] = response.data.pageNo;
      obj['total'] = response.data.totalCount;
      return obj;
    }
  });
}
//认证设备 字典列表
export async function deviceDictList() {
  const type = "APK_COMMAND";
  return request(`/api/sys-dict/list/${type}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
  });
}
//添加设备认证
export async function addDeviceAuth(params: DeviceAuthListItem) {
  return request('/api/system/deviceAuth/add', {
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}

//编辑设备认证
export async function updateDeviceAuth(params: DeviceAuthListItem) {
  return request('/api/system/deviceAuth/update', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}
//版本删除
export async function authDeviceDel(id: number) {
  return request(`/api/system/deviceAuth/delete/${id}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    method: 'delete',
  });
}
/**
 * 
 * 服务管理
 */
//服务管理列表
export async function orgOrderList(params: OrgOrderListItemParams) {
  params = getRandomuserParams(params);
  const res = request('/api/system/org_order/listPage', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params,
  });
  return res.then(function (response) {
    if (response && response.code === 200) {
      const obj = new Object();
      let list = response.data.list;
      for (var i in list) {
        list[i].key = list[i].id;
        list[i].index = parseInt(i)+1;
      }
      obj['data'] = response.data.list;
      obj['pageCount'] = response.data.pageCount;
      obj['current'] = response.data.pageNo;
      obj['total'] = response.data.totalCount;
      return obj;
    }
  });
}
//添加服务
export async function addOrgOrder(params: OrgOrderListItem) {
  return request('/api/system/org_order/add', {
    method: 'POST',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}

//编辑服务
export async function updateOrgOrder(params: OrgOrderListItem) {
  return request('/api/system/org_order/update', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}
//删除服务
export async function orgOrderDel(id: number) {
  return request(`/api/system/org_order/delete/${id}`, {
    headers: {
      Authorization: localStorage.getItem('token'),
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    method: 'delete',
  });
}
/**
 * 
 * 定时发布设置
 */
export async function getPublishTimeSetup(params: any) {
  return request('/api/system/publish_time_setup/getSetInfo', {
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    params
  });
}
//编辑服务
export async function publishTimeSetup(params: setupItem) {
  return request('/api/system/publish_time_setup/setup', {
    method: 'PUT',
    headers: {
      Authorization: localStorage.getItem('token'),
    },
    data: {
      ...params,
    },
  });
}